{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Python可重载运算符\n",
    "\n",
    "所谓重载运算符，指的是在类中定义并实现一个与运算符对应的处理方法，这样当类对象在进行运算符操作时，系统就会调用类中相应的方法来处理"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:pangyazhou;age=35\n",
      "name:pangyazhou;age=35\n",
      "name:pangyazhou;age=35\n",
      "True\n",
      "name:lixiaolong;age=93\n"
     ]
    }
   ],
   "source": [
    "class Person(object):\n",
    "    def __init__(self, name ,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def __str__(self):\n",
    "        return 'name:' + self.name + \";age=\" + str(self.age)\n",
    "\n",
    "    __repr__ = __str__\n",
    "\n",
    "    # 重载 < 运算符\n",
    "    def __lt__(self, other):\n",
    "        if self.age < other.age:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "    # 重载 + 运算符\n",
    "    def __add__(self, other):\n",
    "        return Person(self.name, self.age + other.age)\n",
    "\n",
    "person1 = Person('pangyazhou', 35)\n",
    "person2 = Person('lixiaolong', 58)\n",
    "print(person1)\n",
    "print(repr(person1))\n",
    "print(str(person1))\n",
    "print(person1 < person2)\n",
    "print(person2 + person1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Python 中常用的可重载的运算符，以及各自的含义。\n",
    "<table>\n",
    "<tr><td>重载运算符</td><td>含义</td></tr>\n",
    "<tr><td>__new__</td><td>创建类，在 __init__ 之前创建对象</td></tr>\n",
    "<tr><td>__init__</td><td>类的构造函数，其功能是创建类对象时做初始化工作。</td></tr>\n",
    "<tr><td>__del__</td><td>析构函数，其功能是销毁对象时进行回收资源的操作</td></tr>\n",
    "<tr><td>__add__</td><td>加法运算符 +，当类对象 X 做例如 X+Y 或者 X+=Y 等操作，内部会调用此方法。但如果类中对\n",
    "__iadd__>方法进行了重载，则类对象 X 在做 X+=Y 类似操作时，会优先选择调用 __iadd__ 方法。</td></tr>\n",
    "<tr><td>__radd__</td><td>当类对象 X 做类似 Y+X 的运算时，会调用此方法。</td></tr>\n",
    "<tr><td>__iadd__</td><td>重载 += 运算符，也就是说，当类对象 X 做类似 X+=Y 的操作时，会调用此方法。</td></tr>\n",
    "<tr><td>__or__</td><td>“或”运算符 |，如果没有重载 __ior__，则在类似 X|Y、X|=Y 这样的语句中，“或”符号生效</td></tr>\n",
    "<tr><td>__repr__，__str__</td><td>格式转换方法，分别对应函数 repr(X)、str(X)</td></tr>\n",
    "<tr><td>__call__</td><td>函数调用，类似于 X(*args, **kwargs) 语句</td></tr>\n",
    "<tr><td>__getattr__</td><td>点号运算，用来获取类属性</td></tr>\n",
    "<tr><td>__setattr__</td><td>属性赋值语句，类似于 X.any=value</td></tr>\n",
    "<tr><td>__delattr__</td><td>删除属性，类似于 del X.any</td></tr>\n",
    "<tr><td>__getattribute__</td><td>获取属性，类似于 X.any</td></tr>\n",
    "<tr><td>__getitem__</td><td>索引运算，类似于 X[key]，X[i:j]</td></tr>\n",
    "<tr><td>__setitem__</td><td>索引赋值语句，类似于 X[key], X[i:j]=sequence</td></tr>\n",
    "<tr><td>__delitem__</td><td>索引和分片删除</td></tr>\n",
    "<tr><td>__get__, __set__, __delete__</td><td>描述符属性，类似于 X.attr，X.attr=value，del X.attr</td></tr>\n",
    "<tr><td>__len__</td><td>计算长度，类似于 len(X)</td></tr>\n",
    "<tr><td>__lt__，__gt__，__le__，__ge__，__eq__，__ne__ </td><td>比较，分别对应于 <、>、<=、>=、=、!= 运算符。</td></tr>\n",
    "<tr><td>__iter__，__next__</td><td>迭代环境下，生成迭代器与取下一条，类似于 I=iter(X) 和 next()</td></tr>\n",
    "<tr><td>__contains__</td><td>成员关系测试，类似于 item in X</td></tr>\n",
    "<tr><td>__index__ </td><td>整数值，类似于 hex(X)，bin(X)，oct(X)</td></tr>\n",
    "<tr><td>__enter__，__exit__</td><td>在对类对象执行类似 with obj as var 的操作之前，会先调用 __enter__ 方法，其结果会传给 var；在最终结束该操作之前，会调用 __exit__ 方法（常用于做一些清理、扫尾的工作）</td></tr>\n",
    "</table>"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}